package com.example.hospitalmanagement.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import com.example.hospitalmanagement.BaseApplication
import com.example.hospitalmanagement.bean.Data
import com.example.hospitalmanagement.bean.DepartmentListData
import com.example.hospitalmanagement.bean.ExpertIntroductionData
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class HospitalViewModel : ViewModel() {
    /**
     * 获取科室一二级数据
     */
    fun getDepartment(onDepartmentListener: (dataList: List<Data>) -> Unit) {
        val dataList: MutableList<Data> = mutableListOf()
        BaseApplication.iHospitalRequest.requestDepartment().flatMap {
            Observable.from(it.data)
        }?.map {
            Data(
                it.depSysId,
                it.depSysName,
                it.deskWorks,
                false
            )
        }?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({
                dataList.add(it)
            },
                {
                    Log.e("HospitalViewModel", it.message, it)
                },
                {
                    onDepartmentListener(dataList)
                })
    }

    /**
     * 根据二级科室名获取科室一二级数据
     */
    fun getDepartmentByName(
        searchName: String,
        onDepartmentListener: (dataList: List<Data>) -> Unit
    ) {
        val dataList: MutableList<Data> = mutableListOf()
        BaseApplication.iHospitalRequest.requestDepartmentByName(searchName).flatMap {
            Observable.from(it.data)
        }?.map {
            Data(
                it.depSysId,
                it.depSysName,
                it.deskWorks,
                false
            )
        }?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({
                dataList.add(it)
            },
                {
                    Log.e("HospitalViewModel", it.message, it)
                },
                {
                    onDepartmentListener(dataList)
                })
    }

    /**
     * 获取专家介绍科室列表
     */
    fun getDepartmentList(
        listener: (dataList: MutableList<DepartmentListData>) -> Unit
    ) {
        val dataList: MutableList<DepartmentListData> = mutableListOf()
        BaseApplication.iHospitalRequest.getDepartmentList().flatMap {
            Observable.from(it.data)
        }?.map {
            DepartmentListData(
                depSysId = null,
                deskName = it.deskName,
                deskId = it.deskId,
                deskAddress = null,
                deskTel = null,
                deskIntro = null,
                deskImg = null,
                doctor = null,
                hospId = null,
                checked = false
            )
        }?.subscribeOn(Schedulers.io())
            ?.observeOn(AndroidSchedulers.mainThread())
            ?.subscribe({ dataList.add(it) },
                { Log.e("HospitalViewModel", it.message, it) },
                { listener(dataList) })
    }

    /**
     * 获取专家介绍列表数据
     */
    fun getExpertIntroduction(
        map: MutableMap<String, String>,
        deskWork: MutableMap<String, Array<Int>>?,
        listener: (dataList: MutableList<ExpertIntroductionData>) -> Unit
    ) {
        val dataList: MutableList<ExpertIntroductionData> = mutableListOf()
        deskWork?.let {
            BaseApplication.iHospitalRequest.expertIntroduction(map, it).flatMap {data->
                Observable.from(data.data)
            }.map {data->
                ExpertIntroductionData(
                    docId = data.docId,
                    docImg = data.docImg,
                    docIntro = data.docIntro,
                    docJob = data.docJob,
                    docName = data.docName,
                    deskName = data.deskName
                )
            }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({data-> dataList.add(data) },
                    {e-> Log.e("HospitalViewModel", e.message, e) },
                    { listener(dataList) })
        }
    }
}